<?php

namespace App\Http\Controllers\Labaicai;

use App\Http\Requests\Labaicai\CategoryRequest;
use App\Http\Resources\Labaicai\CategoryResource;
use App\Models\Labaicai\Category;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CategoryController extends Controller
{
    /**
     * Notes:
     * Created on 2024-02-02 01:51:34
     * @param Request $request
     * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
     */
    public function index(Request $request)
    {
        $query = Category::with('dailyFoods')->filter();
        return CategoryResource::collection(
            $request->has('page') ? $query->paginate(per()) : $query->get()
        );
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(CategoryRequest $request)
    {
        Category::query()->create($request->all());
    }

    public function createCategory()
    {
        $data = [
            'name'             => '测试',
            'icon'             => 'test',
            'recommend_in_min' => '1000',
            'recommend_in_max' => '2000',
            'unit'             => '个',
            'type'             => '1',
        ];
        $result = Category::create($data);
    }

    public function updateCategory()
    {
        $data = [
            'name'             => '测试11',
            'icon'             => 'test13',
            'recommend_in_min' => '1000',
            'recommend_in_max' => '2000',
            'unit'             => '个',
            'type'             => '2',
        ];
        $r = Category::where('id', 3)->first()->update($data);

        $category = Category::find(6);
        $result = $category->update($data);
        var_dump($result);
    }

    public function deleteCategory()
    {
        Category::where('id', 6)->first()->delete();
    }

    /**
     * Notes:
     * Created on 2024-02-02 01:51:34
     * @param $id
     * @return CategoryResource
     */
    public function show($id)
    {
        return CategoryResource::make(Category::query()->findOrFail($id));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(CategoryRequest $request, $id)
    {
        Category::where('id', $id)->updateFillable($request->all());
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $m = Category::findOrFail($id);
        $m->delete();
    }

    public function restore()
    {
        Category::withTrashed()->where('id', 1)->restore();
    }
}
